package BLC

import (
	"github.com/boltdb/bolt"
)

type Iterator struct{
	CurrentHash []byte
	DB *bolt.DB
}

func (bc *BlockChain)NewIterator()*Iterator{
	return &Iterator{bc.Tip,bc.DB}
}


//返回当前区块，以及下一个区块的迭代器
func (iter *Iterator)Next()(*Block,*Iterator){
	var block *Block

	err:=iter.DB.View(func(tx *bolt.Tx)error{

		b:=tx.Bucket([]byte(blockBucket))

		blockByte:=b.Get(iter.CurrentHash)

		if blockByte==nil{
			iter=nil
		}else {
			block = UnSerilizeBlock(blockByte)
			//block.Print()
			iter = &Iterator{block.PreviewHash, iter.DB}
		}

		return nil
	})
	if err!=nil{
		panic(err)
	}

	return block,iter
}